module executor.search.SearchUserExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import client.GateServerClient;
import client.MsgServerClient;
import service.SearchUserService;
import service.ApplyService;

/**
查询相关接口
**/

@TransportServer(LogicServerName)
class SearchUserExecutor : AbstractExecutor!(SearchUserExecutor){

  //根据用户名或者手机号或者昵称查找
  @MessageId(MESSAGE.CMD_SEARCH_USER_REQ)
  void searchUser(TransportContext ctx, MessageBuffer buffer) { //支持username mobile nickename 查找
    try {
      auto req = new SearchUserREQ();
      buffer.data.fromProtobuf!SearchUserREQ( req);
      User[] users = SearchUserService.searchUser( req);

      SearchUserRES res = new SearchUserRES;
      res.users = users;
      res.fromId = req.userId;
      res.tagId = req.tagId;
      res.src = req.src;
      //Extend extend;
      //extend.tagId = buffer.extend.tagId;
      ubyte[] extend = buffer.extend;
      ctx.session.send( new MessageBuffer( MESSAGE.CMD_SEARCH_USER_RES,res.toProtobuf.array,extend));
      logInfo( "searchUser ----- ");
    }catch(Exception e)
    {
      logError("error : %s" ,e.msg);
    }
  }

  //根据用户ID数组查找
  @MessageId(MESSAGE.CMD_SEARCH_BY_IDS_REQ)
  void searchByIds(TransportContext ctx, MessageBuffer buffer) { //多个ID查找

    auto req = new SearchByIdsREQ();
    buffer.data.fromProtobuf!SearchByIdsREQ(req);
    User[] users = ApplyService.getUsers(req.ids,req.userId);
    SearchByIdsRES res = new SearchByIdsRES;
    res.users = users;
    res.fromId = req.userId;
    res.src = req.src;
    //Extend extend;
    //extend.tagId = buffer.extend.tagId;
    ubyte[] extend = buffer.extend;
    ctx.session.send(new MessageBuffer(MESSAGE.CMD_SEARCH_BY_IDS_RES,res.toProtobuf.array,extend));
    logInfo("searchByIds ----- ");
  }

  //根据用户ID查找
  @MessageId(MESSAGE.CMD_SEARCH_BY_ID_REQ)
  void searchById(TransportContext ctx, MessageBuffer buffer) { //单个ID 查找

    auto req = new SearchByIdREQ();
    buffer.data.fromProtobuf!SearchByIdREQ(req);
    User user = SearchUserService.selectUser(req.id,req.userId);

    if (user !is null)
    {
      SearchByIdRES res = new SearchByIdRES;
      res.users = user;
      res.fromId = req.userId;
      res.src = req.src;
      //Extend extend;
      //extend.tagId = buffer.extend.tagId;
      ubyte[] extend = buffer.extend;
      ctx.session.send(new MessageBuffer(MESSAGE.CMD_SEARCH_BY_ID_RES,res.toProtobuf.array,extend));
    }
    logInfo("searchById ----- ");
  }
}

